package com.zx.mes.hyl.test.proxy;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * JDK动态代理实现
 * @author hyl
 * @date 2018-2-26
 */
public class JKProxy implements InvocationHandler{
    private static final Logger logger = Logger.getLogger(JKProxy.class);

    private Object target;

    public JKProxy(Object target){
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        logger.info(JSON.toJSONStringWithDateFormat("方法之前打印一些log","yyyy-MM-dd HH:mm:ss"));
        Object result=method.invoke(target,args);
        logger.info(JSON.toJSONStringWithDateFormat("方法之后打印一些log","yyyy-MM-dd HH:mm:ss"));
        return result;
    }
}
